package com.zhz.lib.cms.manage.bookreview;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.zhz.lib.cms.domain.bookreview.Bookreview;
import com.zhz.lib.cms.manage.BaseActionWithApprove;
import com.zhz.lib.cms.utils.zhz.ZhzSecurityUtil;
import com.zhz.lib.cms.utils.zhz.ZhzSortMapUtil;
import com.zhz.project.common.util.alibaba.StringUtil;
import com.zhz.project.common.util.hibernate.BaseDAO;
import com.zhz.project.common.util.zhzutil.ZhzDateUtil;
import com.zhz.project.common.util.zhzutil.ZhzHttpRequestUtil;
import com.zhz.project.common.util.zhzutil.ZhzStringUtil;

public class BookreviewAction extends BaseActionWithApprove {
    private static final int length     = 30;

    private static String    TABLE      = "Bookreview";
    private static String    KEY        = "bookreviewid";
    private static String    LIST       = "bookreviewlist";
    private static String    ObjectName = "bookreview";

    //书评学科系列的 downlist ID
    private Integer          xuekefatherid;
    //新书链接
    private String           isbnlink;

    /**
     * 不给参数，默认执行此处
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     * @see org.apache.struts.actions.DispatchAction#unspecified(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    @Override
    public ActionForward unspecified(ActionMapping mapping, ActionForm form,
                                     HttpServletRequest request, HttpServletResponse response)
                                                                                              throws Exception {
        return bookreviewlist(mapping, form, request, response);
    }

    /**
     * 显示新书简介列表
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward bookreviewlist(ActionMapping mapping, ActionForm form,
                                        HttpServletRequest request, HttpServletResponse response)
                                                                                                 throws Exception {
        StringBuffer fanyeHref = new StringBuffer();
        fanyeHref.append("bookreviewManage.do?method=bookreviewlist");

        List<Bookreview> bookreviewlist = getListWithFanyeHref(request, TABLE,
            BaseDAO.WHERE + BaseDAO.WHERE_DELETE, " order by " + KEY + " DESC", LIST, fanyeHref);
        if (bookreviewlist != null && bookreviewlist.size() > 0) {
            for (Bookreview bookreview : bookreviewlist) {
                if (StringUtil.isNotBlank(bookreview.getBookname()))
                    if (bookreview.getBookname().getBytes().length > length)
                        bookreview.setBookname(ZhzStringUtil.subStringWithDot(bookreview
                            .getBookname(), length));
            }
        }
        return mapping.findForward("bookreview-list");
    }

    /**
     * 添加新书简介时，先执行此函数，取出学科列表，然后传递给jsp
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     */
    public ActionForward bookreviewadd(ActionMapping mapping, ActionForm form,
                                       HttpServletRequest request, HttpServletResponse response) {
        request.setAttribute("xuekelist", ZhzSortMapUtil
            .getChildSortMapList(xuekefatherid, baseDao));
        return mapping.findForward("bookreview-add");
    }

    /**
     * 保存新添加的新书简介
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     */
    public ActionForward bookreviewsave(ActionMapping mapping, ActionForm form,
                                        HttpServletRequest request, HttpServletResponse response) {

        // 获取书名
        String bookname = ZhzHttpRequestUtil.getParameters(request, "bookname");

        // 获取isbn
        String isbn = ZhzHttpRequestUtil.getParameters(request, "isbn");

        // 获取分类号
        String sortNo = ZhzHttpRequestUtil.getParameters(request, "sortNo");

        // 获取学科
        String xueke = ZhzHttpRequestUtil.getParameters(request, "xueke");

        // 获取责任者
        String duty = ZhzHttpRequestUtil.getParameters(request, "duty");

        // 获取出版商
        String publish = ZhzHttpRequestUtil.getParameters(request, "publish");

        // 获取书评内容
        String introduction = ZhzHttpRequestUtil.getParameters(request, "introduction");

        Bookreview bookreview = new Bookreview();

        bookreview.setBookname(bookname);
        bookreview.setIsbn(isbn);
        bookreview.setSortNo(sortNo);
        bookreview.setSubject(xueke);
        bookreview.setDuty(duty);
        bookreview.setRecommend(ZhzSecurityUtil.getOperatorName());
        bookreview.setRecomdate(new Date());
        bookreview.setPublish(publish);
        bookreview.setPublishdate(ZhzDateUtil.getYYYYMDDDate(request.getParameter("publishdate")));
        bookreview.setIntroduction(introduction);
        bookreview.setLink(isbnlink.concat(isbn));
        bookreview.setAproveState(0);
        bookreview.setAproveTime(null);
        bookreview.setEditor(ZhzSecurityUtil.getOperatorName());
        bookreview.setEditorTime(new Date());
        bookreview.setIfDelete(0);
        boolean save = baseDao.save(bookreview);

        if (save) {
            request.setAttribute("addresult", "addresult");
            request.setAttribute("add", "add");
        }

        return mapping.findForward("bookreview-result");
    }

    /**
     * 形成表单
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     */
    public ActionForward bookreviewedit(ActionMapping mapping, ActionForm form,
                                        HttpServletRequest request, HttpServletResponse response) {

        Bookreview bookreview = (Bookreview) getRecord(request, TABLE, KEY, ObjectName);

        request
            .setAttribute("subject", ZhzSortMapUtil.getSortmap(bookreview.getSubject(), baseDao));

        request.setAttribute("xuekelist", ZhzSortMapUtil
            .getChildSortMapList(xuekefatherid, baseDao));

        return mapping.findForward("bookreviewedit-form");
    }

    /**
     * 编辑新书简介提交
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     */
    public ActionForward bookrevieweditsave(ActionMapping mapping, ActionForm form,
                                            HttpServletRequest request, HttpServletResponse response) {

        // 取出表单的值
        String bookreviewid = ZhzHttpRequestUtil.getParameters(request, KEY);

        // 获取书名
        String bookname = ZhzHttpRequestUtil.getParameters(request, "bookname");

        // 获取isbn
        String isbn = ZhzHttpRequestUtil.getParameters(request, "isbn");

        // 获取分类号
        String sortNo = ZhzHttpRequestUtil.getParameters(request, "sortNo");

        // 获取学科
        String xueke = ZhzHttpRequestUtil.getParameters(request, "xueke");

        // 获取责任者
        String duty = ZhzHttpRequestUtil.getParameters(request, "duty");

        // 获取出版商
        String publish = ZhzHttpRequestUtil.getParameters(request, "publish");

        // 获取书评内容
        String introduction = ZhzHttpRequestUtil.getParameters(request, "introduction");

        Bookreview bookreview = (Bookreview) baseDao.findOneObject(TABLE, KEY, bookreviewid);

        bookreview.setBookname(bookname);
        bookreview.setIsbn(isbn);
        bookreview.setSubject(xueke);
        bookreview.setSortNo(sortNo);
        bookreview.setDuty(duty);
        bookreview.setPublish(publish);
        bookreview.setPublishdate(ZhzDateUtil.getYYYYMDDDate(request.getParameter("publishdate")));
        bookreview.setIntroduction(introduction);
        bookreview.setLink(isbnlink.concat(isbn));
        bookreview.setEditor(ZhzSecurityUtil.getOperatorName());
        bookreview.setEditorTime(new Date());

        boolean update = baseDao.update(bookreview);

        if (update) {
            request.setAttribute("editresult", "editresult");
            request.setAttribute("edit", "edit");
        }

        return mapping.findForward("bookreview-result");
    }

    /**
     * 通过ajax请求，获取书评列表
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward bookreviewContent(ActionMapping mapping, ActionForm form,
                                           HttpServletRequest request, HttpServletResponse response)
                                                                                                    throws Exception {
        Bookreview bookreview = (Bookreview) getRecord(request, TABLE, KEY, ObjectName);

        request
            .setAttribute("subject", ZhzSortMapUtil.getSortmap(bookreview.getSubject(), baseDao));

        return mapping.findForward("bookreview-content");
    }

    /**
     * 删除新书简介
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward bookreviewdel(ActionMapping mapping, ActionForm form,
                                       HttpServletRequest request, HttpServletResponse response)
                                                                                                throws Exception {
        delete(request, TABLE, KEY);

        return mapping.findForward("bookreview-result");
    }

    /**
     * 对选择的新书简介进行审核
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward bookreviewApprove(ActionMapping mapping, ActionForm form,
                                           HttpServletRequest request, HttpServletResponse response)
                                                                                                    throws Exception {
        approveRecord(request, TABLE, KEY);
        return mapping.findForward("bookreview-result");
    }

    public void setXuekefatherid(Integer xuekefatherid) {
        this.xuekefatherid = xuekefatherid;
    }

    public void setIsbnlink(String isbnlink) {
        this.isbnlink = isbnlink;
    }

}